package com.yuhanginfo.common.utils;

import javax.servlet.http.HttpServletRequest;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.DeviceType;
import eu.bitwalker.useragentutils.UserAgent;

/**
 * 用户代理字符串识别工具
 * @author YuHangInfo
 * @version 2014-6-13
 */
public class UserAgentUtils {

	private UserAgentUtils() {
	}
	/**
	 * 获取用户代理对象
	 * @param request
	 * @return
	 */
	public static UserAgent getUserAgent(HttpServletRequest request){
		return UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
	}
	
	/**
	 * 获取设备类型
	 * @param request
	 * @return
	 */
	public static DeviceType getDeviceType(HttpServletRequest request){
		return getUserAgent(request).getOperatingSystem().getDeviceType();
	}
	
	/**
	 * 是否是PC
	 * @param request
	 * @return
	 */
	public static boolean isComputer(HttpServletRequest request){
		return DeviceType.COMPUTER.equals(getDeviceType(request));
	}

	/**
	 * 是否是手机
	 * @param request
	 * @return
	 */
	public static boolean isMobile(HttpServletRequest request){
		return DeviceType.MOBILE.equals(getDeviceType(request));
	}
	
	/**
	 * 是否是平板
	 * @param request
	 * @return
	 */
	public static boolean isTablet(HttpServletRequest request){
		return DeviceType.TABLET.equals(getDeviceType(request));
	}

	/**
	 * 是否是手机和平板
	 * @param request
	 * @return
	 */
	public static boolean isMobileOrTablet(HttpServletRequest request){
		DeviceType deviceType = getDeviceType(request);
		return DeviceType.MOBILE.equals(deviceType) || DeviceType.TABLET.equals(deviceType);
	}
	
	/**
	 * 获取浏览类型
	 * @param request
	 * @return
	 */
	public static Browser getBrowser(HttpServletRequest request){
		return getUserAgent(request).getBrowser();
	}
	
	/**
	 * 是否IE版本是否小于等于IE8
	 * @param request
	 * @return
	 */
	public static boolean isLteIE8(HttpServletRequest request){
		Browser browser = getBrowser(request);
		return Browser.IE5.equals(browser) || Browser.IE6.equals(browser)
				|| Browser.IE7.equals(browser) || Browser.IE8.equals(browser);
	}
	
	  public static String getIpAddr(HttpServletRequest request) {  
	        String ip = request.getHeader("X-Real-IP");  
	        if (ip!= null && !"".equals(ip) && !"unknown".equalsIgnoreCase(ip)) {  
	            return ip;  
	        }  
	        ip = request.getHeader("X-Forwarded-For");  
	        if (ip!= null && !"".equals(ip)  && !"unknown".equalsIgnoreCase(ip)) {  
	            // 多次反向代理后会有多个IP值，第一个为真实IP。  
	            int index = ip.indexOf(',');  
	            if (index != -1) {  
	                return ip.substring(0, index);  
	            } else {  
	                return ip;  
	            }  
	        } else {  
	            return request.getRemoteAddr();  
	        }  
	    }  
	      
	    /** 
	     * 获取来访者的浏览器版本 
	     * @param request 
	     * @return 
	     */  
	    public static String getRequestBrowserInfo(HttpServletRequest request){  
	        String browserVersion = null;  
	        String header = request.getHeader("user-agent");  
	        if(header == null || "".equals(header)){  
	           return "";  
	         }  
	        if(header.indexOf("MSIE")>-1){  
	            browserVersion = "IE";  
	        }else if(header.indexOf("Firefox")>-1){  
	            browserVersion = "Firefox";  
	        }else if(header.indexOf("Chrome")>-1){  
	            browserVersion = "Chrome";  
	        }else if(header.indexOf("Safari")>-1){  
	            browserVersion = "Safari";  
	        }else if(header.indexOf("Camino")>-1){  
	            browserVersion = "Camino";  
	        }else if(header.indexOf("Konqueror")>-1){  
	            browserVersion = "Konqueror";  
	        }  
	        return browserVersion;  
	    }  
	  
	    /** 
	     * 获取系统版本信息 
	     * @param request 
	     * @return 
	     */  
	    public static String getRequestSystemInfo(HttpServletRequest request){  
	      String systenInfo = null;  
	      String header = request.getHeader("user-agent");  
	      if(header == null || "".equals(header)){  
	       return "";  
	      }  
	        //得到用户的操作系统  
	        if (header.indexOf("NT 6.0") > -1){  
	            systenInfo = "Windows Vista/Server 2008";  
	        } else if (header.indexOf("NT 5.2") > -1){  
	            systenInfo = "Windows Server 2003";  
	        } else if (header.indexOf("NT 5.1") > -1){  
	            systenInfo = "Windows XP";  
	        } else if (header.indexOf("NT 6.1") > -1){  
	            systenInfo = "Windows 7";  
	        } else if (header.indexOf("NT 6.2") > -1){  
	            systenInfo = "Windows Slate";  
	        } else if (header.indexOf("NT 6.3") > -1){  
	            systenInfo = "Windows 9";  
	        } else if (header.indexOf("NT 5") > -1){  
	            systenInfo = "Windows 2000";  
	        } else if (header.indexOf("NT 4") > -1){  
	            systenInfo = "Windows NT4";  
	        } else if (header.indexOf("Me") > -1){  
	            systenInfo = "Windows Me";  
	        } else if (header.indexOf("98") > -1){  
	            systenInfo = "Windows 98";  
	        } else if (header.indexOf("95") > -1){  
	            systenInfo = "Windows 95";  
	        } else if (header.indexOf("Mac") > -1){  
	            systenInfo = "Mac";  
	        } else if (header.indexOf("Unix") > -1){  
	            systenInfo = "UNIX";  
	        } else if (header.indexOf("Linux") > -1){  
	            systenInfo = "Linux";  
	        } else if (header.indexOf("SunOS") > -1){  
	            systenInfo = "SunOS";  
	        }  
	        return systenInfo;  
	     }  
}
